﻿/* 
 * Since 2008 - 2012
 */
package com.hh.bss.customer.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.BaseSpringController;
import com.hh.bss.common.page.Page;
import com.hh.bss.common.page.PageRequest;
import com.hh.bss.customer.entity.CustomerContacter;
import com.hh.bss.customer.entity.CustomerContractAttachment;
import com.hh.bss.customer.service.CustomerContractAttachmentService;
import com.hh.bss.customer.vo.query.CustomerContractAttachmentQuery;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

@Controller
public class CustomerContractAttachmentController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	private CustomerContractAttachmentService customerContractAttachmentService;

	private CustomerContractAttachment customerContractAttachment;
	java.lang.Integer id = null;
	private String[] items;

	/** 通过spring自动注入 */
	public void setCustomerContractAttachmentServiceImpl(CustomerContractAttachmentService service) {
		this.customerContractAttachmentService = service;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, CustomerContractAttachmentQuery customerContractAttachmentQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			customerContractAttachmentQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = customerContractAttachmentService.findPage(customerContractAttachmentQuery);

		List<CustomerContractAttachment> CustomerContractAttachmentlist = (List) page.getResult();
		ListRange<CustomerContractAttachment> resultList = new ListRange<CustomerContractAttachment>();
		resultList.setRows(CustomerContractAttachmentlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}
	public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			result.put("failure", true);
			result.put("msg", "参数非法!");
		}
		try {
			CustomerContractAttachment data = (CustomerContractAttachment) customerContractAttachmentService.getById(Integer.parseInt(id));
			if (data != null) {
				result.put("data", data);
				result.put("success", true);
				result.put("msg", "成功!");
			}

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}
	 public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			return "forward:/jsp/customer/customerContractAttachment/customerContractAttachmentPage.jsp";
			
     }

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, CustomerContractAttachment customerContractAttachment) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(CustomerContractAttachment.class, "creator")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(customerContractAttachment, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerContractAttachment.class, "createDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(customerContractAttachment, "createDt", new Date());
				} catch (Exception ex) {
				}
			}
			customerContractAttachment.setUploadDt(new Date());
			MultipartHttpServletRequest multipartRequest = null;
			MultipartFile multipartFile = null;
			try {
				multipartRequest = (MultipartHttpServletRequest) request;
				multipartFile = multipartRequest.getFile("uploadFile");
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			// multipart/form-data 提交模式，且提交了文件fileRes,则处理上传文件

			if (multipartRequest != null && multipartFile != null && !multipartFile.isEmpty()) {
				customerContractAttachmentService.saveFile(multipartRequest, multipartRequest, customerContractAttachment);
			}

			customerContractAttachment.setUploadDt(new Date());
			customerContractAttachmentService.save(customerContractAttachment);
			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, CustomerContractAttachment customerContractAttachment) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			CustomerContractAttachment oldcustomerContractAttachment = (CustomerContractAttachment) customerContractAttachmentService.getById(customerContractAttachment.getId());

			bind(request, oldcustomerContractAttachment);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(CustomerContractAttachment.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(oldcustomerContractAttachment, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerContractAttachment.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(oldcustomerContractAttachment, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			MultipartHttpServletRequest multipartRequest = null;
			MultipartFile multipartFile = null;
			try {
				multipartRequest = (MultipartHttpServletRequest) request;
				multipartFile = multipartRequest.getFile("uploadFile");
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			// multipart/form-data 提交模式，且提交了文件fileRes,则处理上传文件

			if (multipartRequest != null && multipartFile != null && !multipartFile.isEmpty()) {
				customerContractAttachmentService.saveFile(multipartRequest, multipartRequest, oldcustomerContractAttachment);
			}

			customerContractAttachmentService.update(oldcustomerContractAttachment);
			result.put("success", true);
			result.put("msg", "修改成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				customerContractAttachmentService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

}
